home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / kgstr.pas < prev    next >
Pascal/Delphi Source File  |  1992-12-26  |  5KB  |  107 lines

  1. Unit KgStr;
  2. interface
  3.  
  4. {This unit defines some generally useful utilities for manipluating
  5. strings in Turbo Pascal 6.0 or greater. Unless noted otherwise, the
  6. routines in this unit will function in either Real or Protected mode.
  7. This unit is under constant development of course, and will be
  8. frequently updated as more routines are added.
  9.  
  10.   Author: Keith Greer
  11.           68 Tamworth Rd.
  12.           Troy, OH  45373-1551
  13.  
  14.   C'Serve ID: 73457,3042
  15.     Internet: greerk@wpdis11.hq.aflc.af.mil
  16.  
  17. }
  18.  
  19. function FixLen(AnyString:string; PadChar: char; FldSize:word) :string;
  20. function SpaceOut(AnyString:string) :string;
  21.  
  22. implementation
  23.  
  24. {///////////////////////// FixLen ///////////////////////////
  25.  
  26. FixLen takes a string and pads it with PadChar until the result is
  27. FldSize in length. This is typically useful for appending period leaders
  28. (.....) to the end of a string, or creating dividers by passing a nul
  29. string: FixLen('','=',80) for example.}
  30.  
  31. function FixLen(AnyString:string; PadChar: char; FldSize:word) :string;
  32.                                                              assembler;
  33. asm
  34.         PUSH    DS              {Save Data Segment}
  35.         CLD                     {Clear direction flag}
  36.         LDS     SI,AnyString    {DS:SI-->AnyString}
  37.         LES     DI,@Result      {ES:DI-->String to be returned}
  38.         MOV     BX,DI           {Save DI value for later}
  39.         LODSB                   {AL has Length(AnyString)}
  40.         CBW                     {Make AL into word in AX}
  41.         STOSB                   {Put the length into Result & Inc(DI)}
  42.         MOV     CX,AX           {Length in CX}
  43.         REP     MOVSB           {Pad=AnyString}
  44.         MOV     CX,FldSize      {CX has FldSize}
  45.         XOR     CH,CH           {Make FldSize=FldSize mod 256}
  46.         MOV     ES:[BX],CL      {Make Length(Pad)=FldSize}
  47.         SUB     CX,AX           {CX=FldSize-Length(AnyString)}
  48.         JB      @1              {Return truncated string if CX<0}
  49.         MOV     AL,PadChar      {else load character to pad}
  50.         REP     STOSB           {and pad to FldSize}
  51. @1:                             {Go back}
  52.         POP     DS              {Restore Data Segment}
  53. end;
  54.  
  55. {//////////////////////// SpaceOut /////////////////////////
  56.  
  57. SpaceOut trims the leading and trailing spaces from AnyString and
  58. returns the result. Useful for bulletproofing your user interface.}
  59.  
  60. function SpaceOut(AnyString:string) :string; assembler;
  61. asm
  62.         PUSH    DS              {Save Data Segment}
  63.         CLD                     {Clear direction flag (search forward)}
  64.         LES     DI,AnyString    {ES:DI-->AnyString}
  65.         MOV     AL,ES:[DI]      {Get the length of AnyString}
  66.         CBW                     {Make it a word}
  67.         MOV     CX,AX
  68.         JCXZ    @1              {AnyString was ''!}
  69.         INC     DI              {ES:DI-->First char of AnyString}
  70.         MOV     DX,DI           {DX has first char offset}
  71.         ADD     DX,AX           {Add the length}
  72.         MOV     AL,' '          {What to look for}
  73.         REPE    SCASB           {Keep looking till out of spaces or string}
  74.         JE      @1              {Had a string full of spaces, jump}
  75.         DEC     DI              {Move back to the first non-blank}
  76.         MOV     SI,DI           {and put the offset into SI}
  77.         STD                     {Search backwards}
  78.         MOV     DI,DX
  79.         DEC     DI              {DI->last character}
  80.         MOV     CX,DX           {Calculate how many characters to search}
  81.         SUB     CX,SI
  82.         JCXZ    @3              {None to search}
  83.         REPE    SCASB           {Do it}
  84.         JE      @1              {All spaces}
  85.         INC     DI              {DI->last non-blank character}
  86.         INC     DI
  87.         MOV     DX,DI           {Set new EOS}
  88. @3:
  89.         PUSH    ES
  90.         POP     DS              {DS:SI-->First non-blank in AnyString}
  91.         MOV     AX,DX           {AX has offset of last char}
  92.         SUB     AX,SI           {minus current offset}
  93.         LES     DI,@Result      {ES:DI-->Result}
  94.         CLD                     {Make sure we go forward again}
  95.         STOSB                   {Save the length into Result}
  96.         MOV     CX,AX
  97.         REP     MOVSB           {Get the remainder of AnyString into Result}
  98.         JMP     @2              {All done}
  99. @1:
  100.         LES     DI,@Result
  101.         MOV BYTE PTR ES:[DI],0  {Make Result=''}
  102. @2:                             {Go back}
  103.         POP     DS              {Restore Data Segment}
  104. end;
  105.  
  106. end.
  107.